// packageA/pages/myInfo/index.js

const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      real_name:'',
      mobile:'',
      region:'',
      introduction:'',
      id_code_positive:'',
      id_code_back:'',
      real_image_url:[],
      medical_care_image_url:[],
      id_code:'',
      sex:'',
      id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(e.detail.value[0],e.detail.value[1])
      this.setData({
          region: e.detail.value[0]+','+e.detail.value[1]
      })
  },

  uploadImage(e) {
    var that = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        const token = wx.getStorageSync('token')
        console.log(token)
        wx.uploadFile({
          url: 'https://kpz.qixiaocheng.com/index.php?s=/api/upload/image&wxapp_id=' + App.getWxappId() + '&token=' + token,
          filePath: tempFilePaths[0],
          name: 'iFile',
          formData: {
            'user': 'test'
          },
          success(res) {
            const data = JSON.parse(res.data)
            console.log(data, e.currentTarget.dataset.id)
            if (data.code == '-1') {
              that.doLogin(1)
            } else {
              if(e.currentTarget.dataset.id==1){
                that.setData({
                  id_code_positive:data.data.file_path
                })
              }else if(e.currentTarget.dataset.id==2){
                that.setData({
                  id_code_back:data.data.file_path
                })
              }else if(e.currentTarget.dataset.id==3){
                  const url={
                    url:data.data.file_path
                  }
                  that.data.real_image_url.push({url:url.url})
                  that.setData({
                    real_image_url:that.data.real_image_url
                  })
                  console.log(data.data.file_path,that.data.real_image_url)
              }else if(e.currentTarget.dataset.id==4){
                  const url={
                    url:data.data.file_path
                  }
                  that.data.medical_care_image_url.push({url:url.url})
                  that.setData({
                    medical_care_image_url:that.data.medical_care_image_url
                  })
                  console.log(data.data.file_path,that.data.medical_care_image_url)
              }
              
            }
          }
        })
      }
    })
  },

  s_btn(){
    console.log(123)
    wx.redirectTo({
      url: '/packageA/pages/user/index',
    })
  },
  getInfo(){
    let _this = this;
    App._get('accompany_info/detail', {
      user_id:wx.getStorageSync('user_id')
    }, result => {
      // 设置顶部导航栏栏
      // _this.setData({
      //   peopleList:result.data.list
      // });
      console.log(result)
      _this.setData({
        real_name:result.data.detail.real_name,
        mobile:result.data.detail.mobile,
        region:result.data.region[0]+','+result.data.region[1],
        introduction:result.data.detail.introduction,
        id_code_positive:result.data.detail.id_code_positive,
        id_code_back:result.data.detail.id_code_back,
        real_image_url:result.data.detail.real_image_url?result.data.detail.real_image_url:[],
        medical_care_image_url:result.data.detail.medical_care_image_url?result.data.detail.medical_care_image_url:[],
        id_code:result.data.detail.id_code,
        sex:result.data.detail.sex,
        id:result.data.detail.id
      })
    });
  },


  // 修改
  getUpdate(){
    console.log(this.data)
    const token = wx.getStorageSync('token')
    console.log(token)
    App._post_form('accompany_info/edit', {
      real_name:this.data.real_name,
      mobile:this.data.mobile,
      region:this.data.region,
      introduction:this.data.introduction,
      id_code_positive:this.data.id_code_positive,
      id_code_back:this.data.id_code_back,
      real_image_url:JSON.stringify(this.data.real_image_url)  ,
      medical_care_image_url: JSON.stringify(this.data.medical_care_image_url)  ,
      id:this.data.id
    }, result => {
        // wx.redirectTo({
        //   url: '/packageA/pages/user/index',
        // })
      wx.navigateBack()
      console.log(result)
      
    })
  },


  bindHidereal_name(e){
    this.setData({
      real_name:e.detail.value
    })
  },
  bindHidemobile(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  bindHideid_code(e){
    this.setData({
      id_code:e.detail.value
    })
  },
  bindHideintroduction(e){
    this.setData({
      introduction:e.detail.value
    })
  }
  
})